package com.example.smartall.ui;

 /**
  * @Description:    description
  * @Author:         abbas
  * @CreateDate:
  */
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;

import com.example.smartall.R;
import com.example.smartall.adapter.OrderSearchResultListAdapter;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.OrderSearchResultBean;
import com.example.smartall.ui.base.BaseActivity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class OrderSearchResultActivity extends BaseActivity {

    private String key;
    private ListView lvFood;

    private List<OrderSearchResultBean.RowsBean> beans;
    private OrderSearchResultListAdapter adapter;

    private MHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_search_result);

        key = getIntent().getStringExtra("key");
        handler = new MHandler();

        init();
        getFoodData();
        initEvent();
    }

    @Override
    public void init() {
        lvFood = (ListView) findViewById(R.id.lv_food);

    }

    @Override
    public void initEvent() {

    }

    private void getFoodData(){
        HashMap<String,Object> params = new HashMap<>();
        params.put("keyword",key);
        params.put("pageNum",1);
        params.put("pageSize",8);
        Api.config(ApiConfig.ORDER_SEARCH_LIST,params).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                OrderSearchResultBean bean = new Gson().fromJson(res,OrderSearchResultBean.class);
                if (bean.getCode() == 200){
                    beans = bean.getRows();
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    class MHandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0x01:
                    List<OrderSearchResultBean.RowsBean.ProductListBean> data = new ArrayList<>();
                    for (int i = 0 ; i < beans.size() ; i++){
                        data.addAll(beans.get(i).getProductList());
                    }
                    adapter = new OrderSearchResultListAdapter(OrderSearchResultActivity.this,data);
                    lvFood.setAdapter(adapter);
                    break;
                default:
                    break;
            }
        }
    }
}